
class Node{
    public int val;
    public Node left;
    public Node right;
    public Node(int val){
        this.val=val;
    }
}
public class BinarySearchTree {
    public Node root=null;

    public static void func(Node root){
        if(root==null){
            return;
        }
    }
    public Node search(int key){
        Node cur=root;
        while (cur!=null){
            if(cur.val<key){
                cur=cur.right;
            }else if(cur.val==key){
                return cur;
            }else {
                cur=cur.left;
            }
        }
        return null;//代表没有数据
    }
    public boolean insert(int val){
        if(root==null){
            root=new Node(val);
            return true;
        }
        Node cur=root;
        Node parent=null;
        while (cur!=null){

            if(cur.val<val){
                parent=cur;
                cur=cur.right;

            }else if(cur.val>val){
                parent=cur;
                cur=cur.left;

            }else {
                return false;
            }
        }
        if(parent.val>val){
            parent.left=new Node(val);
        }else {
            parent.right=new Node(val);
        }
        return true;
    }
    public void remove(int key){
        Node cur=root;
        Node parent=null;
        while (cur!=null){
            if(cur.val==key){
                removeNode(parent,cur);
                break;
            }else if(cur.val<key){
                parent=cur;
                cur=cur.right;
            }else {
                parent=cur;
                cur=cur.left;
            }
        }
    }
    public void removeNode(Node parent,Node cur){
        if(cur.left==null){
            if (cur==root){
                cur=cur.right;
            }else if(cur==parent.left){
                parent.left=cur.right;
            }else {
                parent.right=cur.right;
            }
        }else if(cur.right==null){
            if(cur==root){
                root=cur.left;
            }else if(cur==parent.left){
                parent.left=cur.left;
            }else {
                parent.right=cur.left;
            }
        }else {
            Node targetParent=cur;
            Node target=cur.right;
            while (target.left!=null){
                targetParent=target;
                target=target.left;
            }
            cur.val=target.val;
            if(target==targetParent.left){
                targetParent.left=target.right;
            }else {
                targetParent.right=target.right;
            }
        }
    }

    public void inOrder(Node root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    public static void main(String[] args) {
        int[] array={10,8,19,3,9,4,7};
        BinarySearchTree binarySearchTree=new BinarySearchTree();
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println(binarySearchTree.insert(20));
        binarySearchTree.inOrder(binarySearchTree.root);

        System.out.println("删除数据");
        binarySearchTree.remove(3);
        binarySearchTree.inOrder(binarySearchTree.root);
    }
}
